package com.lwj.liwanjia.view.activity.myindent;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.AddAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.bean.PingjiaBean;
import com.lwj.liwanjia.impl.PingjiaView;
import com.lwj.liwanjia.presenter.PingjiaPresenter;
import com.lwj.liwanjia.presenter.UrlPresenter;
import com.lwj.liwanjia.utils.BitmapUtils;
import com.lwj.liwanjia.utils.InputImageDialog;
import com.lwj.liwanjia.utils.RatingBar;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo.TuiKuanDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShangChengPjActivity extends BaseActivity implements PingjiaView {


    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_shangpin)
    ImageView ivShangpin;
    @BindView(R.id.shangpin_rating_bar)
    RatingBar shangpinRatingBar;
    @BindView(R.id.tv_shangpin_pingjia)
    TextView tvShangpinPingjia;
    @BindView(R.id.ll_shangpin)
    LinearLayout llShangpin;
    @BindView(R.id.dianpu_rating_bar)
    RatingBar dianpuRatingBar;
    @BindView(R.id.tv_dianpu_pingjia)
    TextView tvDianpuPingjia;
    @BindView(R.id.wuliu_rating_bar)
    RatingBar wuliuRatingBar;
    @BindView(R.id.tv_wuliu_pingjia)
    TextView tvWuliuPingjia;
    @BindView(R.id.ed_miaoshu)
    EditText edMiaoshu;
    @BindView(R.id.rl_add)
    SuperRecyclerView rlAdd;

    private List<String> list = new ArrayList<>();
    private AddAdapter addAdapter;
    private PingjiaPresenter pingjiaPresenter;//评价
    private String token;
    private InputImageDialog imageDialog;
    private View footerView;
    private List<File> files = new ArrayList<>();
    private String compressPath = Environment.getExternalStorageDirectory().getPath() + "/compress/";
    private Map<String, Object> map = new HashMap<>();
    private MyIndentBean.DataBean.GoodsBean.CommodityBean commodityBean;
    private String tradeId;
    private ImageView add;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("评价");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("发布");
        MyIndentBean.DataBean.GoodsBean goods = (MyIndentBean.DataBean.GoodsBean) getIntent().getSerializableExtra("infor");
        tradeId = goods.getTradeId();
        commodityBean = goods.getCommodity().get(0);
        Glide.with(this).load(commodityBean.getCommodityimage()).error(R.mipmap.tupian).into(ivShangpin);
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        rlAdd.setLayoutManager(manager);
        rlAdd.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.left = getResources().getDimensionPixelOffset(R.dimen.x10);
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.x10);
            }
        });
        rlAdd.setRefreshEnabled(false);
        rlAdd.setLoadMoreEnabled(false);
        addAdapter = new AddAdapter(this, list);
        addView();
        rlAdd.setAdapter(addAdapter);
        pingjiaPresenter = new PingjiaPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();
        listener();
    }

    private void listener() {
        shangpinRatingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                setPingjia(ratingCount, tvShangpinPingjia);
            }
        });
        dianpuRatingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                setPingjia(ratingCount, tvDianpuPingjia);
            }
        });
        wuliuRatingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                setPingjia(ratingCount, tvWuliuPingjia);
            }
        });
        addAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                list.remove(position);
                if (list.size() < 4) {
                    add.setVisibility(View.VISIBLE);
                }
                addAdapter.notifyDataSetChanged();
            }
        });
    }

    private void addView() {
        footerView = LayoutInflater.from(this).inflate(R.layout.add_item, null);
        add = (ImageView) footerView.findViewById(R.id.iv_add);
        addAdapter.addFooterView(footerView);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageDialog = new InputImageDialog(ShangChengPjActivity.this);
                imageDialog.show();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shang_cheng_pj;
    }

    @OnClick({R.id.tv_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                for (int i = 0; i < list.size(); i++) {
                    String imageUrl = list.get(i);
                    String fileName = System.currentTimeMillis() + ".png";
                    BitmapUtils.compressImageByPixel(imageUrl, fileName);
                    File file = new File(compressPath + fileName);
                    files.add(file);
                }
                getMap();
                if (TextUtils.isEmpty(edMiaoshu.getText().toString())) {
                    Toast.makeText(this, "请给宝贝描述一下吧", Toast.LENGTH_SHORT).show();
                    return;
                }
                map.put("content", edMiaoshu.getText().toString());
                pingjiaPresenter.pingjiaIndent(map, files);
                break;
        }
    }

    private void getMap() {
        map.put("token", token);
        map.put("commodityType", commodityBean.getCommodityType());
        map.put("commodityCode", commodityBean.getCommoditycode());
        map.put("goodsGrade", shangpinRatingBar.getStarCount());
        map.put("shopGrade", dianpuRatingBar.getStarCount());
        map.put("expressGrade", wuliuRatingBar.getStarCount());
        map.put("orderId", tradeId);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            //照相
            if (requestCode == InputImageDialog.CAMERA_REQUEST_CODE) {
                String imagePath = imageDialog.cameraPath;
                if (list.size() < 4) {
                    list.add(imagePath);
                }
                if (list.size() == 4) {
                    add.setVisibility(View.GONE);
                }
            } else if (requestCode == InputImageDialog.ALBUM_REQUEST_CODE) {//相册
                ArrayList<String> imageUrl = data.getStringArrayListExtra("imageUrl");
                for (int i = 0; i < imageUrl.size(); i++) {
                    if (list.size() < 4) {
                        list.add(imageUrl.get(i));
                    }
                    if (list.size() == 4) {
                        add.setVisibility(View.GONE);
                    }
                }
            }
            addAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void pingjiaIndent(PingjiaBean pingjiaBean) {
        if (pingjiaBean.getState() == 0) {
            Toast.makeText(this, "评价失败", Toast.LENGTH_SHORT).show();
        } else if (pingjiaBean.getState() == 1) {
            if (pingjiaBean.isData()) {
                Toast.makeText(this, "评价成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    private void setPingjia(float ratingCount, TextView tv) {
        switch (ratingCount + "") {
            case "1.0":
                tv.setText("差");
                break;
            case "2.0":
                tv.setText("一般");
                break;
            case "3.0":
                tv.setText("好");
                break;
            case "4.0":
                tv.setText("很好");
                break;
            case "5.0":
                tv.setText("非常好");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pingjiaPresenter != null) {
            pingjiaPresenter = null;
        }
    }
}
